import axios from 'axios';
import { AxiosRequestConfig } from 'axios';
import qs from 'query-string';

/**
 * 提现状态枚举
 */
export enum WithdrawStatus {
  APPLYING = 0,    // 申请中
  APPROVED = 1,    // 审核通过
  PAID = 2,        // 已打款
  COMPLETED = 3,   // 已完成
  REJECTED = 4     // 已拒绝
}

/**
 * 打款方式枚举
 */
export enum PaymentMethod {
  BANK = 1,        // 银行转账
  WECHAT = 2,      // 微信
  ALIPAY = 3       // 支付宝
}

/**
 * 提现记录查询参数
 */
export interface PartnerWithdrawQuery {
  pageNum: number;
  pageSize: number;
  partnerId?: number;
  partnerName?: string;
  withdrawNo?: string;
  withdrawStatus?: number;
  paymentMethod?: number;
  startTime?: string;
  endTime?: string;
}

/**
 * 提现记录数据模型
 */
export interface PartnerWithdrawVO {
  id?: number;
  withdrawNo?: string;
  partnerId?: number;
  partnerName?: string;
  withdrawAmount?: number;
  serviceFee?: number;
  feeRate?: number;  // 手续费率
  actualAmount?: number;
  withdrawStatus?: number;
  withdrawStatusName?: string;
  paymentMethod?: number;
  paymentMethodName?: string;
  paymentAccount?: string;
  paymentName?: string;
  paymentTime?: string;
  paymentVoucher?: string;
  paymentOperator?: string;  // 打款操作人
  reviewBy?: string;
  reviewTime?: string;
  reviewRemark?: string;
  reviewer?: string;  // 审核人
  completeTime?: string;  // 完成时间
  completeOperator?: string;  // 完成操作人
  remark?: string;
  // 银行转账信息
  bankName?: string;
  bankAccount?: string;
  bankBranch?: string;
  // 支付宝信息
  alipayAccount?: string;
  // 微信信息
  wechatAccount?: string;
  createTime?: string;
  updateTime?: string;
}

/**
 * 提现记录导入数据模型
 */
export interface PartnerWithdrawImportVO {
  partnerName: string;
  withdrawAmount: number;
  paymentMethod: number;
  paymentAccount: string;
  paymentName: string;
  remark?: string;
}

/**
 * 获取运营商提现记录分页列表
 */
export function getPartnerWithdrawPage(params: PartnerWithdrawQuery) {
  return axios.post('/api/partner/withdraw/page', params);
}

/**
 * 获取提现记录详情
 */
export function getPartnerWithdrawDetail(id: number) {
  return axios.get(`/api/partner/withdraw/${id}`);
}

/**
 * 新增提现记录
 */
export function createPartnerWithdraw(data: PartnerWithdrawVO) {
  return axios.post('/api/partner/withdraw', data);
}

/**
 * 更新提现记录
 */
export function updatePartnerWithdraw(data: PartnerWithdrawVO) {
  return axios.put('/api/partner/withdraw', data);
}

/**
 * 审核提现申请
 */
export function reviewPartnerWithdraw(id: number, approved: boolean, reviewRemark?: string) {
  return axios.post(`/api/partner/withdraw/review/${id}`, null, {
    params: {
      approved,
      reviewRemark
    },
    paramsSerializer: (params) => {
      return qs.stringify(params);
    }
  });
}

/**
 * 确认打款
 */
export function confirmPayment(id: number, paymentVoucher: string) {
  return axios.post(`/api/partner/withdraw/confirm-payment/${id}`, null, {
    params: {
      paymentVoucher
    }
  });
}

/**
 * 完成提现
 */
export function completeWithdraw(id: number) {
  return axios.post(`/api/partner/withdraw/complete/${id}`);
}

/**
 * 删除提现记录
 */
export function deletePartnerWithdraw(id: number) {
  return axios.delete(`/api/partner/withdraw/${id}`);
}

/**
 * 批量删除提现记录
 */
export function batchDeletePartnerWithdraw(ids: number[]) {
  return axios.delete('/api/partner/withdraw/batch', {
    data: ids
  });
}

/**
 * 导入提现记录
 */
export function importPartnerWithdraw(data: PartnerWithdrawImportVO[]) {
  return axios.post('/api/partner/withdraw/import', data);
}

/**
 * 导出提现记录
 */
export function exportPartnerWithdraw(params: PartnerWithdrawQuery) {
  return axios.post('/api/partner/withdraw/export', params, {
    responseType: 'blob'
  } as AxiosRequestConfig);
} 